//
//  LoginRegisterViewController.swift
//  HLSwiftWork
//
//  Created by hulinhu on 2018/6/15.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit
import Alamofire

class LoginRegisterViewController: UIViewController {

    var  loginRgisterBtn :UIButton!
    var  loginRegBtn:   UIButton!
    var  forgetPassBtn :UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupLoginRgisterUI()
        setupThirdLoginUI()
        
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view .endEditing(true)
    }

}

extension LoginRegisterViewController {
    
    func setupUI()  {
        
        let bgImageView = UIImageView()
        bgImageView.isUserInteractionEnabled = true
        bgImageView.image = UIImage.init(named: "login_register_background")
        view .addSubview(bgImageView)
        bgImageView.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.view)
            make.top.bottom.equalTo(self.view)
        }
        
        // 返回按钮
        let backBtn = UIButton.init(type: .custom)
        backBtn.setImage(UIImage.init(named: "login_close_icon"), for: .normal )
        backBtn.addTarget(self, action: #selector(backBtnClicked), for: .touchUpInside)
        view.addSubview(backBtn)
        backBtn.snp.makeConstraints { (make) in
//            make.top.equalTo(self.view).offset(kTopHeight-10)
            make.topMargin.equalTo(self.view).offset(kTopHeight-10)
            make.left.equalTo(self.view).offset(20)
            make.width.height.equalTo(16)
        }
        
        // 登录注册按钮
        loginRgisterBtn = UIButton.init(type: .custom)
        loginRgisterBtn.setTitle("注册账号", for:.normal)
        loginRgisterBtn.addTarget(self, action: #selector(loginRgisterBtnClick(_:)), for: .touchUpInside)
        view.addSubview(loginRgisterBtn)
        loginRgisterBtn.snp.makeConstraints { (make) in
            make.topMargin.equalTo(backBtn.snp.top).offset(0)
            make.rightMargin.equalTo(self.view).offset(0)
            make.width.equalTo(80)
        }
        
        
    }
    
    func setupLoginRgisterUI() {
        
        let sView = UIView()
        sView.backgroundColor = UIColor.clear
        view.addSubview(sView)
        sView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(loginRgisterBtn.snp.bottom).offset(50)
            make.height.equalTo(200)
        }
        
        // 登录背景图
        let bgImgeV = UIImageView.init(image: UIImage.init(named: "login_rgister_textfield_bg"))
        bgImgeV.isUserInteractionEnabled = true
        sView .addSubview(bgImgeV)
        bgImgeV.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(0)
            make.centerX.equalTo(sView)
            make.height.equalTo(92)
            make.width.equalTo(266)
        }
        
        // 手机号
        let sHeight = 46
//            Int(bgImgeV.snp.height*0.5)

        let phoneFiled = HLTextField()
        phoneFiled.placeholder = "请输入手机号"
        phoneFiled.textColor = UIColor.white
        bgImgeV .addSubview(phoneFiled)
    
        phoneFiled.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(0)
            make.centerX.equalTo(bgImgeV)
            make.height.equalToSuperview().multipliedBy(0.5)
            make.width.equalToSuperview()
        }
        
        // 密码
        let passFiled = HLTextField()
        passFiled.placeholder = "请输入密码"
        passFiled.textColor = UIColor.white
        bgImgeV .addSubview(passFiled)
        
        passFiled.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(sHeight)
            make.centerX.equalTo(bgImgeV)
            make.height.equalToSuperview().multipliedBy(0.5)
            make.width.equalToSuperview()
        }
        
        // 登录/注册按钮
        loginRegBtn = UIButton.init(type: .custom)
        loginRegBtn.setBackgroundImage(UIImage.init(named: "login_register_button"), for: .normal )
        loginRegBtn.setTitle("登录", for:.normal)
        loginRegBtn.setTitleColor(UIColor.white, for: .normal )
        loginRegBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        view.addSubview(loginRegBtn)
        loginRegBtn.snp.makeConstraints { (make) in
            make.top.equalTo(bgImgeV.snp.bottom).offset(20)
            make.centerX.equalTo(bgImgeV)
            make.width.equalTo(bgImgeV)
        }
        
        
        // 登录注册按钮
        forgetPassBtn = UIButton.init(type: .custom)
        forgetPassBtn.setTitle("忘记密码?", for:.normal)
        forgetPassBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        forgetPassBtn.addTarget(self, action: #selector(forgetPassBtnClick), for: .touchUpInside)
        sView.addSubview(forgetPassBtn)
        forgetPassBtn.snp.makeConstraints { (make) in
            make.top.equalTo(loginRegBtn.snp.bottom).offset(20)
            make.right.equalTo(bgImgeV).offset(0)
            make.width.equalTo(80)
        }
        
    }
    
    func setupThirdLoginUI(){
        
        let sView = UIView()
        sView.backgroundColor = UIColor.clear
        view.addSubview(sView)
        sView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(self.view)
            make.height.equalTo(160)
        }
        
        let slabel = UILabel()
        slabel.text = "快速登录"
        slabel.backgroundColor = UIColor.clear
        slabel.textColor = UIColor.lightGray
        slabel.font = UIFont.systemFont(ofSize: 18)
        sView .addSubview(slabel)
    
        slabel.snp.makeConstraints { (make) in
            make.top.equalTo(sView).offset(0)
            make.centerX.equalTo(self.view)
            make.height.equalTo(30)
        }
        
        let leftImagView = UIImageView()
        leftImagView.image = UIImage.init(named: "login_register_left_line")
        sView .addSubview(leftImagView)
        leftImagView.snp.makeConstraints { (make) in
            make.top.equalTo(slabel.snp.top).offset(15)
            make.right.equalTo(slabel.snp.left).offset(-10)
            make.width.equalTo(103)
            make.height.equalTo(1)
        }
        
        let rightImagView = UIImageView()
        rightImagView.image = UIImage.init(named: "login_register_right_line")
        sView .addSubview(rightImagView)
        rightImagView.snp.makeConstraints { (make) in
            make.top.equalTo(slabel.snp.top).offset(15)
            make.left.equalTo(slabel.snp.right).offset(10)
            make.width.height.equalTo(leftImagView)
        }
        
        let titles = ["QQ登录","新浪微博","腾讯微博"]
        let images = ["login_QQ_icon","login_sina_icon","login_tecent_icon"]
        for index in 0..<3 {
            let button = MineSqurebtn.init(type: .custom)
            button.tag = 1000+index
            button.setTitle(titles[index], for: .normal)
            button.setImage(UIImage.init(named: images[index]) , for: .normal)
            button.setTitleColor(UIColor.darkGray, for: .normal)
            button.setTitleColor(UIColor.red, for: .disabled)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.addTarget(self, action: #selector(ThirdLoginBtnClick(_:)), for: .touchUpInside)
            sView .addSubview(button)
            button.snp.makeConstraints({ (make) in
                make.left.equalTo(80+index*90)
                make.top.equalTo(45)
                make.width.equalTo(70)
                make.height.equalTo(90)
            })
        }
    }
    
}


extension LoginRegisterViewController  {
    
    @objc fileprivate func backBtnClicked(){
        dismiss(animated: true, completion: nil)
    }
    
    @objc fileprivate func loginRgisterBtnClick(_ sender : UIButton){
        
        sender.isSelected = !sender.isSelected
        
        if sender.isSelected {
            sender.setTitle("登录账号", for: .normal)
            loginRegBtn.setTitle("注册", for: .normal)
            forgetPassBtn.isHidden = true
        }else {
            sender.setTitle("注册账号", for: .normal)
            loginRegBtn.setTitle("登录", for: .normal)
            forgetPassBtn.isHidden = false
        }
    }
    
    @objc fileprivate func loginBtnClick(){
        NSLog(" loginBtnClick")
        
    }
    @objc fileprivate func forgetPassBtnClick(){
        NSLog(" forgetPassBtnClick")
        
    }
    
    @objc fileprivate func ThirdLoginBtnClick(_ sender : UIButton){
        switch sender.tag {
        case 1000:
            print("QQ按钮")
        case 1001:
            print("微博按钮")
        case 1002:
            print("腾讯微博按钮")
        default:
            print("no way")
        }
        
    }
    
    
    
    
}
